<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/17
 * Time: 下午12:49
 */

namespace app\macht\controller;


use app\common\model\Agent;
use app\common\model\CardMember;
use app\common\model\CardQrcode;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use Endroid\QrCode\QrCode;
use think\Controller;
use think\Image;
use think\Request;
use think\Session;

class Qrcodes extends Controller
{
    public function qrcode($mid = 602, $url = 'https://wxpay.weilinzhou.com/index.php?s=/Index/pay/id/1.html', $level = 4, $size = 16)
    {
        $map['id'] = $mid;
        $Merchant = new Merchant();
        $merchant = $Merchant->where($map)->field('merchant')->find();
        $qrCode = new QrCode();
        $qrCode->setText($url)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();
        $path = $_SERVER['DOCUMENT_ROOT'] . "/qrcode/" . $mid . '.png';
        file_put_contents($path, $image);
        return $path;
    }
    public function sq_qrcode($mid,$url){
        $qrCode = new QrCode();
        $qrCode->setText($url)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();
        $path = $_SERVER['DOCUMENT_ROOT'] . "/sqqrcode/" . $mid . '.png';
        file_put_contents($path, $image);
        return "/sqqrcode/" . $mid . '.png';


    }

    //门店收款码带背景
    public function getmachpaybgimg($mid){
        //
        $Merchant = new Merchant();
        $Agent = new Agent();
        $mach = $Merchant->where(['id'=>$mid])->field('agent')->find();
        $agentinfo = $Agent->where(['id'=>$mach['agent']])->field('name,tel')->find();
        //获取二维码
        $qr = $this->qrcode($mid);

        //保存路径
        $qr2 = $_SERVER['DOCUMENT_ROOT'] . "/qrcode/" ."bg_". $mid . '.png';
        //背景图
        $bg_img = $_SERVER['DOCUMENT_ROOT'].'/static/img/mach_pay_bg.png';
        //打开背景图
        $img = \think\Image::open($bg_img);
        //加上电话
        $img->text('技术支持 · '.$agentinfo['name'].' '.$agentinfo['tel'], $_SERVER['DOCUMENT_ROOT'].'/static/font/vistaYahei.ttf',24,'#000000',\think\Image::WATER_SOUTHWEST,array(322, -40))->save($qr2);
        //合成图片
        $imgs = \think\Image::open($qr2);
        $imgs->water($qr, array(318, 475))->save($qr2);
        $qr_return = "/qrcode/" ."bg_". $mid . '.png';
        return $qr_return;
    }

    //门店开卡一键二维码带背景
    public function getmachcardbgimg($mid){
        //保存路径
        $qr2 = $_SERVER['DOCUMENT_ROOT'] . "/bizcard/qrcode/" ."bg_". $mid . '.png';
        if (file_exists($qr2)) {
            $qr_return = "/bizcard/qrcode/" ."bg_". $mid . '.png';
            return $qr_return;
        }
        //获取商家信息
        $Momerchant = new Momerchant();
        $sql['id'] = Session::get('login_top_mid');
        $moinfo = $Momerchant->where($sql)->field('id,merchant_name,img')->find();
        $domains = request()->domain();
        $logo_url = str_replace($domains,$_SERVER['DOCUMENT_ROOT'],$moinfo['img']);
        $merchant_name = $moinfo['merchant_name'];
        //背景图
        $bg_img = $_SERVER['DOCUMENT_ROOT'].'/static/img/mach_card_bg.png';

        //打开LOGO
        $image = \think\Image::open($logo_url);
        //生成缩略图
        $mologo = $_SERVER['DOCUMENT_ROOT'] . "/momach/thumb/" ."bg_". $moinfo['id'] . '.png';
        $image->thumb(110, 110)->save($mologo);
        //打开背景图
        $img = \think\Image::open($bg_img);
        //合成图片  logo
        $img->water($mologo, array(470, 1490))->save($qr2);
        //打开图片
        $imgs = \think\Image::open($qr2);
        //合成图片  字
        $imgs->text($merchant_name, $_SERVER['DOCUMENT_ROOT'].'/static/font/vistaYahei.ttf',42,'#CCCCCC',\think\Image::WATER_SOUTHWEST,array(610, -160))->save($qr2);
//        exit();
        //获取二维码
        $CardQrcode = new CardQrcode();
        $map['mid'] = $mid;
        $map['type'] = 1;
        $info = $CardQrcode->where($map)->find();
        if ($info){
            //二维码
            $qr = $_SERVER['DOCUMENT_ROOT'] . "/bizcard/" . $info['id'] . '.png';
            //合成图片
            $imgs->water($qr, array(150, 710))->save($qr2);

            $qr_return = "/bizcard/qrcode/" ."bg_". $mid . '.png';
        }else{
            $qr_return = '';
        }
        return $qr_return;
    }

    //A-4纸平铺背景  4*1
    public function getafprint($img_address,$mid,$type=''){
        if ($img_address) {
            if ($type=='cardus') {
                //返回路径
                $qr_return = "/a4down/cardus/" . "bg_a4_" . $mid . '.png';
                //保存路径
                $qr2 = $_SERVER['DOCUMENT_ROOT'] . "/a4down/cardus/" . "bg_a4_" . $mid . '.png';
                if (file_exists($qr2)) {
                    return $qr_return;
                }
                $qr = $_SERVER['DOCUMENT_ROOT'] . $img_address;
                $bg_img = $_SERVER['DOCUMENT_ROOT'] . '/static/img/a4_bg.png';

                //打开背景图 1
                $img = \think\Image::open($bg_img);
                //合成图片 1
                $img->water($qr, array(160, 40))->save($qr2);
                //打开背景图 2
                $img = \think\Image::open($qr2);
                //合成图片 2
                $img->water($qr, array(160, 1780))->save($qr2);
                //打开背景图 3
                $img = \think\Image::open($qr2);
                //合成图片 3
                $img->water($qr, array(1270, 1780))->save($qr2);
                //打开背景图 4
                $img = \think\Image::open($qr2);
                //合成图片 4
                $img->water($qr, array(1270, 40))->save($qr2);
                //END
            }else{
                //返回路径
                $qr_return = "/a4down/pay/" . "bg_a4_" . $mid . '.png';
                //保存路径
                $qr2 = $_SERVER['DOCUMENT_ROOT'] . "/a4down/pay/" . "bg_a4_" . $mid . '.png';
                if (file_exists($qr2)) {
                    return $qr_return;
                }
                $qr = $_SERVER['DOCUMENT_ROOT'] . $img_address;
                $bg_img = $_SERVER['DOCUMENT_ROOT'] . '/static/img/a4_bg.png';

                //打开背景图 1
                $img = \think\Image::open($bg_img);
                //合成图片 1
                $img->water($qr, array(40, 20))->save($qr2);
                //打开背景图 2
                $img = \think\Image::open($qr2);
                //合成图片 2
                $img->water($qr, array(40, 1760))->save($qr2);
                //打开背景图 3
                $img = \think\Image::open($qr2);
                //合成图片 3
                $img->water($qr, array(1250, 1760))->save($qr2);
                //打开背景图 4
                $img = \think\Image::open($qr2);
                //合成图片 4
                $img->water($qr, array(1250, 20))->save($qr2);
                //END
            }
        }else{
            $qr_return = '';
        }
        return $qr_return;
    }




    function getcodeimage($url,$save_dir='',$filename='',$type=0){
        if(trim($url)==''){
            return array('file_name'=>'','save_path'=>'','error'=>1);
        }
        if(trim($save_dir)==''){
            $save_dir='./';
        }
        if(trim($filename)==''){//保存文件名
            $ext=strrchr($url,'.');
            if($ext!='.gif'&&$ext!='.jpg'){
                return array('file_name'=>'','save_path'=>$_SERVER['DOCUMENT_ROOT'] . "/qrcode/getcard/",'error'=>3);
            }
            $filename=time().$ext;
        }
        if(0!==strrpos($save_dir,'/')){
            $save_dir.='/';
        }
        //创建保存目录
        if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
            return array('file_name'=>'','save_path'=>$_SERVER['DOCUMENT_ROOT'] . "/qrcode/getcard/",'error'=>5);
        }
        //获取远程文件所采用的方法
        if($type){
            $ch=curl_init();
            $timeout=5;
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $img=curl_exec($ch);
            curl_close($ch);
        }else{
            ob_start();
            readfile($url);
            $img=ob_get_contents();
            ob_end_clean();
        }
        //$size=strlen($img);
        //文件大小
        $fp2=@fopen($save_dir.$filename,'a');
        fwrite($fp2,$img);
        fclose($fp2);
        unset($img,$url);
        return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
    }

    //生成二维码
    public function testqrcode(){
        $arr = 'http://weixin.qq.com/q/024LbWgy9gab_1-2oThr4x';
        $res = $this->getcodeimage($arr);
        var_dump($res);
    }

    //卡券二维码
    public function cardqrcode($arr, $level = 4, $size = 16)
    {
        $qrCode = new QrCode();
        $qrCode->setText($arr)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();

        $path = $_SERVER['DOCUMENT_ROOT'] . "/qrcode/card/" . $arr . '.png';
        file_put_contents($path, $image);
        $res = request()->domain() . "/qrcode/card/" . $arr . '.png';
        return $res;
    }

    public function cardbarcode($arr ='704210880124', $level = 4, $size = 16)
    {

        $qrCode = new QrCode();
        $qrCode->setText($arr)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();

        $path = $_SERVER['DOCUMENT_ROOT'] . "/barcode/card/" . $arr . '.png';
        file_put_contents($path, $image);
        $res = request()->domain() . "/barcode/card/" . $arr . '.png';
        return $res;
    }

    //开卡组件二维码
    public function cardbizqrcode($id,$url = 'https://os.wunengkeji.com', $level = 4, $size = 16){
//        return $id;
        $qrCode = new QrCode();
        $qrCode->setText($url)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();
        $path = $_SERVER['DOCUMENT_ROOT'] . "/bizcard/" . $id . '.png';
        file_put_contents($path, $image);
//        $res =  request()->domain() .  "/biz/card/" . $id . '.png';
        return $path;
    }

    public function mercode($mid = 602, $size = 48, $url = 'https://wxpay.weilinzhou.com/index.php?s=/Index/pay/id/1.html', $merchant = "智慧林州")
    {
        $Merchant=new Merchant();
        $merchant=$Merchant->where(['id'=>$mid])->value('merchant');
        $len = strlen($merchant);
            if ($len >= 6 && $len <= 27) {
                switch ($len) {
                    case 9:
                        $merx = 500;
                        break;
                    case 12:
                        $merx = 470;
                        break;
                    case 15:
                        $merx = 440;
                        break;
                    case 18:
                        $merx = 410;
                        break;
                    case 21:
                        $merx = 380;
                        break;
                    case 24:
                        $merx = 350;
                        break;
                    case 27:
                        $merx = 320;
                        break;
                    default:
                        $merx = 530;
                }
            } elseif ($len > 27) {

                $size = 36;

                switch ($len) {
                    case 28:
                        $merx = 370;
                        break;
                    case 29:
                        $merx = 360;
                        break;
                    case 30:
                        $merx = 360;
                        break;
                    case 31:
                        $merx = 345;
                        break;
                    case 32:
                        $merx = 335;
                        break;
                    case 33:
                        $merx = 330;
                        break;
                    case 34:
                        $merx = 325;
                        break;
                    case 35:
                        $merx = 320;
                        break;
                    default:
                        $merx = 530;
                }
            }
        $qr=$this->qrcode($mid, $url);
        $user_pic = $qr;
        $img = \think\Image::open('./assets/footer.png');
        $qr2 = $_SERVER['DOCUMENT_ROOT'] . "/mercode/" . $mid . '.png';
        $s=$img->water($user_pic,array(300, 420))->text($merchant, VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf', $size, '#000000', array($merx, 1020))->save($qr2);
        return $qr2;
    }
}